package com.example.egghttp;

import com.example.egghttp.retrofit.interceptor.CheckSign;
import com.example.egghttp.retrofit.interceptor.GeneraterSign;
import com.example.egghttp.retrofit.interceptor.Login;
import com.example.egghttp.retrofit.interceptor.Token;
import com.example.egghttp.retrofit.interceptor.pda.CommonPDAHeader;

import me.jessyan.progressmanager.ProgressManager;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

    private static Retrofit retrofit = null;
    private static Retrofit retrofitup = null;

    public static void setDomain(String domain) {
        RetrofitClient.domain = domain;
    }

    //    private static String domain = "https://api.sandan.store/";
//    public static String N = "http://api.sandan.store/";
//    public static String T = "http://api-t.sandan.store/";
    public static String AuerdiTest = "http://api.auerdi.cn/v1/";//api.auerdi.cn
    public static String domain = AuerdiTest;
//    public static String domain =T;
//    public static String domain = "https://api.sandan.store/";


    public static Retrofit getPDAClient() {
        if (retrofit == null) {

            final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

            httpClient.addInterceptor(new CommonPDAHeader());
            httpClient.addInterceptor(new Token());
            httpClient.addInterceptor(new GeneraterSign());
            httpClient.addInterceptor(new Login());

            CheckSign interceptor = new CheckSign();
            interceptor.setLevel(CheckSign.Level.BODY);
            httpClient.addInterceptor(interceptor).build();

            OkHttpClient client = httpClient.build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(getDomain())
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();


        }
        return retrofit;
    }

    public static Retrofit getClient() {
        if (retrofit == null) {


            final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

            httpClient.addInterceptor(new CommonPDAHeader());
            httpClient.addInterceptor(new Token());
            httpClient.addInterceptor(new GeneraterSign());
            httpClient.addInterceptor(new Login());

            CheckSign interceptor = new CheckSign();
            interceptor.setLevel(CheckSign.Level.BODY);
            httpClient.addInterceptor(interceptor).build();

            OkHttpClient client = httpClient.build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(getDomain())
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();


        }
        return retrofit;
    }

    public static String getDomain() {
        return domain;
    }

    private static String getStatus() {
        return domain;
    }


    public static Retrofit getPDAClientUP() {
        if (retrofitup == null) {


            final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

            ProgressManager.getInstance().with(httpClient)
                    .build();

            httpClient.addInterceptor(new CommonPDAHeader());
            httpClient.addInterceptor(new Token());
//            httpClient.addInterceptor(new GeneraterSign());
//            httpClient.addInterceptor(new Login());

            CheckSign interceptor = new CheckSign();
            interceptor.setLevel(CheckSign.Level.BODY);
            httpClient.addInterceptor(interceptor).build();

            OkHttpClient client = httpClient.build();
            retrofitup = new Retrofit.Builder()
                    .baseUrl(getDomain())
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();


        }
        return retrofitup;
    }
}
